package com.example.pharmacy.service;

import com.example.pharmacy.mapper.OrderInfoMapper;
import com.example.pharmacy.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-21
 * Time: 20:17
 */
@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserHistoryOrderService userHistoryOrderService;
    public List<OrderResult> getOrdersByname(String userName) {
        List<OrderResult> orderResults = new ArrayList<>();
        try {
            UserInfo userInfo = userService.selectUserByName(userName);
            List<OrderInfo> orderInfos = orderInfoMapper.getOrderInfos(userInfo.getUserId());
            for (OrderInfo orderInfo :
                    orderInfos) {
                Integer orderId = orderInfo.getOrderId();
                List<UserHistoryOrder> userHistoryOrder = userHistoryOrderService.selectByOrderId(orderId);

                OrderResult orderResult = new OrderResult();
                orderResult.setOrderId(orderId);
                orderResult.setAllAmount(orderInfo.getTotalAmount());
                orderResult.setCreateTime(orderInfo.getCreateTime());
                orderResult.setOrderList(userHistoryOrder);

                orderResults.add(orderResult);
            }
        } catch (Exception e) {
            log.error("查找历史订单出错, e:{}", e.getMessage());
        }
        return orderResults;
    }
    public Integer updateOrder(OrderInfo orderInfo) {
        return orderInfoMapper.updateOrder(orderInfo);
    }
}
